package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityReceiptInfoBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.ReceiptEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.ShopEvent;
import com.sgb.kjwl.view.ui.adapter.self.ReceiptInfoAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.ReceiptInfoModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class ReceiptInfoActivity extends AppActivity {
    private ActivityReceiptInfoBinding binding;
    private ReceiptInfoAdapter adapter;
    private List<ReceiptEntity> stringList;
    private Map deletePerson;
    @Inject
    ReceiptInfoModel viewModel;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ReceiptInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_receipt_info);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        binding.include.titleView.setText("收款账号");
        binding.include.rightView.setText("新增收款账号");
        binding.include.rightView.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        if (adapter == null) {
            adapter = new ReceiptInfoAdapter(getActivity(), null);
        }
        binding.rlvList.setLayoutManager(layoutManager);
        binding.rlvList.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<ReceiptEntity>() {
            @Override
            public void onItemClick(ReceiptEntity s, int position) {
            }
        });
        adapter.IClick(new ReceiptInfoAdapter.IClick() {
            @Override
            public void onDelete(String id, int position) {
                showDialog(id, position);
            }

            @Override
            public void isCheck(boolean isCheck, int position) {
                stringList.get(position).setDefault(true);
                viewModel.receiptUpdate(stringList.get(position));
            }
        });
        binding.rlvList.setLoadingMoreEnabled(false);
        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
//                viewModel.refresh(map);
                viewModel.receiptList();
            }

            @Override
            public void onLoadMore() {
                viewModel.lodaMore();
            }
        });


    }


    @Override
    protected void init() {
        SharedPreferenceUtil.init(this);
        deletePerson = new HashMap();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.receiptList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                ReceiptInfoEditActivity.start(this, null);
                break;
        }
    }


    public void refresh(List<ReceiptEntity> stringList) {
        this.stringList = stringList;
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvList.setVisibility(View.VISIBLE);
        binding.rlvList.setLoadingMoreEnabled(true);
        adapter.setDatas(stringList);
        binding.rlvList.refreshComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }


    public void loadMore(List<ReceiptEntity> stringList) {
        adapter.addDatas(stringList);
        binding.rlvList.loadMoreComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rlvList.refreshComplete();
        adapter.setDatas(null);
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    private void showDialog(String id, int position) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("删除收款账号");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText("是否删除收款账号");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口
//                deletePerson.put("id", id);
                viewModel.receiptDelete(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setTextColor(getResources().getColor(R.color.color_333333));
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public void deleteSucc() {
        MToast.showToast(this, "删除成功");
        EventBus.getDefault().postSticky("REFRESH_COMPANY_INFO");
        viewModel.receiptList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(ShopEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_RECEIPT_COMMIT) {
            viewModel.receiptList();
            EventBus.getDefault().removeStickyEvent(event);
        }
    }
}
